﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.UsersFile;


namespace Butcher.winform.EnterpriseForms.RBAC
{
    public partial class UserAdd : Form
    {
        List<int> roles = new List<int>();
        public static int count = 0;
        UICheckBox[] checkbox = new UICheckBox[count];
        public UserAdd()
        {
            InitializeComponent();
        }
        public class RoleSearch
        {
            public string? UserName { get; set; } = null;
        };

        private void button1_Click(object sender, EventArgs e)
        {
            string urr = @"http://localhost:5021/api/Trace/AddUser";
            HttpClient client = new HttpClient();
            UserCreateDTO useradd = new UserCreateDTO();
            useradd.UserName = textBox1.Text;
            useradd.Pwd = textBox3.Text;
            useradd.Stuct = Convert.ToInt32(textBox2.Text);
            for (int i = 0; i < count; i++)
            {
                if (checkbox[i].Checked == true)
                {
                    roles.Add(Convert.ToInt32(checkbox[i].Name));
                }
            }
            useradd.Ids = roles;
            var content = new StringContent(JsonSerializer.Serialize<UserCreateDTO>(useradd));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(urr, content).Result.Content.ReadAsStringAsync().Result;
            if (result != null)
            {
                MessageBox.Show("新增成功");

            }
        }

        private void uiFlowLayoutPanel1_Load(object sender, EventArgs e)
        {
            int counts = 0;
            Roles str = new Roles();

            string url = @"http://localhost:5021/api/Trace/GetRoles";
            HttpClient client = new HttpClient();

            string result = client.GetStringAsync(url).Result;
            JsonSerializerOptions options = new JsonSerializerOptions();
            options.PropertyNameCaseInsensitive = true;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"].Value<JArray>();
            List<Roles> ssss = results.ToObject<List<Roles>>();

            count=ssss.Count;   
            for (int i = 0; i < checkbox.Length; i++)
            {
                checkbox[i] = new UICheckBox();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                checkbox[i].Location = new Point(i * 130 + 130, 30);
                Roles asas = ssss[i];
                //转换为字符串
                checkbox[i].Name = Json.Serialize(asas);
                //转换为实体
                Roles babab = Json.Deserialize<Roles>(checkbox[i].Name);
                checkbox[i].Size = new Size(120, 30);
                checkbox[i].BackColor = Color.White;
                checkbox[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                checkbox[i].Text = ssss[i].RolesName;
                checkbox[i].Name = ssss[i].RolesId.ToString();
                
                //checkbox[i].Click += new System.EventHandler(this.button_Click);
                uiFlowLayoutPanel1.Controls.Add(checkbox[i]);
            }
        }
    }
}
